<?php

namespace app\common\controller;

class Base extends \think\Controller{

	protected $uid = '';
	protected $adm = '';
	protected $admin_info = [];

	protected $data_uids_string ='' ;
	protected $data_uids_arr = [];
	/*
	初始化
	*/
	public function initialize(){
		//登录检测
		$user_info = session('user_info');
		if (session('ip') != request()->ip()) {
			session_destroy();
		}
		if (empty($user_info)) {
			//echo '1';
			$this->redirect('admin/Login/index');
			exit;
		} else {
			//echo '2';
			$uid         = $user_info['id'];
			$user_name   = $user_info['name'];
			$url   = request()->baseUrl();
			$param = request()->param();
			// $uid   = isset($param['uid'])?$param['uid']:'';
			$data  = '';
			unset($param[$url]);
			unset($param['action']);
			unset($param['func']);
			unset($param['api']);
			// var_dump($param);die();
			foreach ($param as $key => $val) {
				if ($url == '/admin/authority/edit' || $url == '/admin/authority/edit.html') {
					if ($key == 'nav') {
						$val = implode($val, ',');
					}
				}
				// var_dump($url);die();
				$data .= $key.'='.$val.' & ';
			}
			$ip = request()->ip();
			// $this->ip = $ip;
			if ($url != '/admin/index/newMessage') {
				model('IpadminModel')->addData($uid,$ip,$url,$data);
			}
			
			$admin = $this->isAdmin($uid,$user_name);
			if (empty($admin)) {
				//echo '3';
				exit;
			}
			$this->uid = $uid;
			$this->adm = $user_name;
			$this->admin_info = $admin;
			if ($admin['data_uid']) {
				$this->data_uids_arr = db('tp_user_team')->where('uid',$admin['data_uid'])->column('x_uid');
				$this->data_uids_arr[] = $admin['data_uid'];
				$this->data_uids_string = implode(',', $this->data_uids_arr);
				// $this->data_all_uids = $this->data_uids;
			} else {
				$this->data_uids_arr = db('tp_user')->where('id','>',0)->column('id');
				// $this->data_uids = implode(',', $this->data_all_uids);
			}
			$controller = strtolower(request()->controller());
			$action     = request()->action();
			$nav_id = db('tp_admin_nav')->where('controller',$controller)->where('action',$action)->field('id,pid')->find();
			if ($admin['authority'] == 0){
				// $controller = request()->controller();
				// $action     = request()->action();
				$nav = db('tp_admin_nav')->where('pid',0)->where('is_use',1)->order('sort asc')->select();
				foreach ($nav as $key => $val) {
					$nav[$key]['nav_list'] = db('tp_admin_nav')->where('pid',$val['id'])->where('is_use',1)->select();
				}
			} else {
				if ($nav_id) {
					$auth = db('tp_admin_authority')->where('c_id',$nav_id['pid'])->where('a_id',$nav_id['id'])->find();

					if (empty($auth)) {
						var_dump($nav_id['pid']);
						var_dump($nav_id['id']);
						exit('权限不足');
					}
				}
				
				//权限管理
				$nav_ids = db('tp_admin_authority')->where('uid',$uid)->column('c_id');
				$nav = db('tp_admin_nav')->where('pid',0)->where('is_use',1)->where('id','in',$nav_ids)->order('sort asc')->select();
				foreach ($nav as $key => $val) {
					$nav2_ids = db('tp_admin_authority')->where('uid',$uid)->where('c_id',$val['id'])->column('a_id');
					$nav[$key]['nav_list'] = db('tp_admin_nav')->where('id','in',$nav2_ids)->where('is_use',1)->select();
				}

			}
			
			if (empty($nav_id)) {
				$nav_id = 0;
			}
			$this->assign('nav_id',$nav_id);
			$this->assign('nav',$nav);
			$this->assign('uid',$uid);
			$this->assign('user_info',$admin);
		}
		
	}
	/*
	是否管理员账号
	 */
	protected function isAdmin($id='',$name=''){
		$res = db('tp_admin')->where('id',$id)->where('name',$name)->find();
		return $res;
	}
	/*
	加密
	 */
	protected function encrypt($str=''){
		$en_str = $str;
		return $en_str;
	}
	/*
	解密
	 */
	protected function decrypt($str=''){
		$de_str = $str;
		return $de_str;
	}
	/**
	 * [imageOpt description]
	 * @param  [type] $obj      图片对象
	 * @param  [type] $path     保存盘
	 * @param  [type] $validate 验证规则
	 * @return [type]           [description]
	 */
	protected function imageOpt($obj,$path,$validate){
		$res = $obj->validate($validate)->move(ROOT_PATH . 'public' . DS . $path);
		if ($res) {
            $name = $res->getSaveName(); 
            $name = str_replace('\\', '/', $name);
            $name = "/".$path.'/'.$name;
            return $name;
        } else {
            return false;
        }
	}
	/*
	表格数据返回
	 */
	public function tableData($data=''){
		if ($data) {
			return ['code'=>0,'msg'=>'','count'=>$data['total'],'data'=>$data['data']];
		} else {
			return ['code'=>1,'msg'=>'数据为空或获取失败'];
		}
	}
}